import uuid
from django.db import models


class Category(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)  # 使用 UUID 作为主键
    name = models.CharField(max_length=100, unique=True)  # 类别名称，确保唯一
    description = models.TextField(blank=True, null=True)  # 类别描述
    created_at = models.DateTimeField(auto_now_add=True)  # 创建时间
    updated_at = models.DateTimeField(auto_now=True)  # 更新时间
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True,
                               related_name='subcategories')  # 自关联，用于表示父类别

    def __str__(self):
        return self.name

    @property
    def is_leaf(self):
        return not self.subcategories.exists()  # 判断该类别是否是叶子节点


class VideoSeries(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)  # 使用 UUID 作为主键
    title = models.CharField(max_length=255)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='video_series')
    subcategory = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='video_subseries', null=True,
                                    blank=True)  # 二级类别
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    cover_image = models.URLField(blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True)
    release_date = models.DateField(null=True, blank=True)
    status = models.CharField(max_length=50, choices=[('ongoing', 'Ongoing'), ('completed', 'Completed')],
                              default='completed')

    def __str__(self):
        return f"{self.title} ({self.category.name} - {self.subcategory.name if self.subcategory else 'No Subcategory'})"


class VideoEpisode(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)  # 使用 UUID 作为主键
    series = models.ForeignKey(VideoSeries, on_delete=models.CASCADE, related_name='episodes')  # 外键关联到 VideoSeries
    episode_number = models.IntegerField()  # 集数编号
    title = models.CharField(max_length=255)  # 集数标题
    video_url = models.URLField()  # 视频链接
    duration = models.DurationField()  # 视频时长
    aired_date = models.DateField(null=True, blank=True)  # 播出日期
    views_count = models.PositiveIntegerField(default=0)  # 观看次数，默认是0

    class Meta:
        unique_together = ('series', 'episode_number')  # 确保同一个系列内集数不重复

    def __str__(self):
        return f"{self.series.title} - Episode {self.episode_number}"


class Resource(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)  # 使用 UUID 作为主键
    episode = models.ForeignKey(VideoEpisode, on_delete=models.CASCADE, related_name='resources')  # 外键关联到 VideoEpisode
    resource_url = models.URLField()  # 资源链接
    resource_type = models.CharField(max_length=50)  # 资源类型，比如：高清、普通等
    quality = models.CharField(max_length=50, blank=True, null=True)  # 清晰度（如：1080p、720p等）
    file_size = models.BigIntegerField(null=True, blank=True)  # 文件大小，单位为字节（字节数可能比较大）
    resource_name = models.CharField(max_length=255, blank=True, null=True)  # 资源文件的名称（例如，资源文件的文件名）

    def __str__(self):
        return f"{self.resource_type} - {self.episode.title}"
